#include "GlobalInstanceSet.h"
#include "RegionInstanceSet.h"
#include "AlarmInstanceSet.h"
#include "EventInstanceSet.h"
#include "DeviceInstanceSet.h"
#include "StatInstanceSet.h"
#include "AccountInstanceSet.h"
#include "common.h"
#include "AgentInstanceSet.h"
#include "RepositoryInstanceSet.h"
#include "DefectInstanceSet.h"
#include "WhitelistitemInstanceSet.h"
#include "AuditInstanceSet.h"
#include "AssetInstanceSet.h"
bool CreateCommonInstanceSet()
{
    if(g_pCommonInstanceSet == NULL)
    {
        g_pCommonInstanceSet = new CommonInstanceSet;
    }
    return (g_pCommonInstanceSet != NULL);
}

void DestroyCommonInstanceSet()
{
    SAFE_DELETE(g_pCommonInstanceSet);
}

bool CreateDbInstanceSet()
{
    if(g_pDbInstanceSet == NULL)
    {
        g_pDbInstanceSet = new DbInstanceSet();
    }
    return (g_pDbInstanceSet != NULL);
}

void DestroyDbInstanceSet()
{
    SAFE_DELETE(g_pDbInstanceSet);
}

bool CreateLoginInstanceSet()
{
    if(g_pLoginInstanceSet == NULL)
    {
        g_pLoginInstanceSet = new LoginInstanceSet();
    }
    return (g_pLoginInstanceSet != NULL);
}

void DestroyLoginInstanceSet()
{
    SAFE_DELETE(g_pLoginInstanceSet);
}

bool CreateAccountInstanceSet()
{
    if(g_pAccountInstanceSet == NULL)
    {
        Database* pDb = g_pDbInstanceSet->GetDatabase();
        g_pAccountInstanceSet = new AccountInstanceSet(pDb);
    }
    return (g_pAccountInstanceSet != NULL);
}

void DestroyAccountInstanceSet()
{
    SAFE_DELETE(g_pAccountInstanceSet);
}

bool CreateRegionInstanceSet(Database* pDb)
{
    if(g_pRegionInstanceSet == NULL)
    {
        g_pRegionInstanceSet = new RegionInstanceSet(pDb);
    }
    return (g_pRegionInstanceSet != NULL);
}

void DestroyRegionInstanceSet()
{
    SAFE_DELETE(g_pRegionInstanceSet);
}

bool CreateAlarmInstanceSet(Database* pDb)
{
    if(g_pAlarmInstanceSet == NULL)
    {
        g_pAlarmInstanceSet = new AlarmInstanceSet(pDb);
    }
    return (g_pAlarmInstanceSet != NULL);
}

void DestroyAlarmInstanceSet()
{
    SAFE_DELETE(g_pAlarmInstanceSet);
}

bool CreateEventInstanceSet(Database* pDb)
{
	if(g_pEventInstanceSet == NULL)
	{
		g_pEventInstanceSet = new EventInstanceSet(pDb);
	}
	return (g_pEventInstanceSet != NULL);
}

void DestroyEventInstanceSet()
{
	SAFE_DELETE(g_pEventInstanceSet);
}

bool CreateDeviceInstanceSet(Database* pDb)
{
    if(g_pDeviceInstanceSet == NULL)
    {
        g_pDeviceInstanceSet = new DeviceInstanceSet(pDb);
    }
    return (g_pDeviceInstanceSet != NULL);
}

void DestroyDeviceInstanceSet()
{
    SAFE_DELETE(g_pDeviceInstanceSet);
}

bool CreateStatInstanceSet(Database* pDb)
{
    if(g_pStatInstanceSet == NULL)
    {
        g_pStatInstanceSet = new StatInstanceSet(pDb);
    }
    return (g_pStatInstanceSet != NULL);
}

void DestroyStatInstanceSet()
{
    SAFE_DELETE(g_pStatInstanceSet);
}


bool CreateAgentInstanceSet(Database* pDb)   
{
     if(g_pAgentInstanceSet == NULL)
    {
        
		g_pAgentInstanceSet = new AgentInstanceSet(pDb);
    }
    return (g_pAgentInstanceSet != NULL);
}

void DestroyAgentInstanceSet()
{
    SAFE_DELETE(g_pAgentInstanceSet);
}

bool CreateRepositoryInstanceSet(Database* pDb)   
{
     if(g_pRepositoryInstanceSet == NULL)
    {
        
		g_pRepositoryInstanceSet = new RepositoryInstanceSet(pDb);
    }
    return (g_pRepositoryInstanceSet != NULL);
}

void DestroyRepositoryInstanceSet()
{
    SAFE_DELETE(g_pRepositoryInstanceSet);
}

bool CreateDefectInstanceSet(Database* pDb)   
{
     if(g_pDefectInstanceSet == NULL)
    {
        
		g_pDefectInstanceSet = new DefectInstanceSet(pDb);
    }
    return (g_pDefectInstanceSet != NULL);
}

void DestroyDefectInstanceSet()
{
    SAFE_DELETE(g_pDefectInstanceSet);
}

bool CreateWhitelistitemInstanceSet(Database* pDb)   
{
     if(g_pWhitelistitemInstanceSet == NULL)
    {
        
		g_pWhitelistitemInstanceSet = new WhitelistitemInstanceSet(pDb);
    }
    return (g_pWhitelistitemInstanceSet != NULL);
}

void DestroyWhitelistitemInstanceSet()
{
    SAFE_DELETE(g_pWhitelistitemInstanceSet);
}

bool CreateAuditInstanceSet(Database* pDb)   
{
     if(g_pAuditInstanceSet == NULL)
    {
        
		g_pAuditInstanceSet = new AuditInstanceSet(pDb);
    }
    return (g_pAuditInstanceSet != NULL);
}

void DestroyAuditInstanceSet()
{
    SAFE_DELETE(g_pAuditInstanceSet);
}

bool CreateAssetInstanceSet(Database* pDb)   
{
     if(g_pAssetInstanceSet == NULL)
    {
        
		g_pAssetInstanceSet = new AssetInstanceSet(pDb);
    }
    return (g_pAssetInstanceSet != NULL);
}

void DestroyAssetInstanceSet()
{
    SAFE_DELETE(g_pAssetInstanceSet);
}
bool CreateOtherInstanceSets()
{
    if(g_pDbInstanceSet != NULL)
    {
        Database* pDb = g_pDbInstanceSet->GetDatabase();
        if (CreateRegionInstanceSet(pDb)
            && CreateAlarmInstanceSet(pDb)
			&& CreateEventInstanceSet(pDb)
            && CreateDeviceInstanceSet(pDb)
            && CreateStatInstanceSet(pDb)&&CreateAgentInstanceSet(pDb)&&CreateRepositoryInstanceSet(pDb)&&CreateDefectInstanceSet(pDb)
			&&CreateWhitelistitemInstanceSet(pDb)&&CreateAuditInstanceSet(pDb)&&CreateAssetInstanceSet(pDb))
        {
            return true;
        }
    }
    return false;
}

void DestroyOtherInstanceSets()
{
    SAFE_DELETE(g_pStatInstanceSet);
    SAFE_DELETE(g_pDeviceInstanceSet);
    SAFE_DELETE(g_pAlarmInstanceSet);
    SAFE_DELETE(g_pRegionInstanceSet);
	SAFE_DELETE(g_pAgentInstanceSet);
	SAFE_DELETE(g_pRepositoryInstanceSet);
	SAFE_DELETE(g_pDefectInstanceSet);
	SAFE_DELETE(g_pWhitelistitemInstanceSet);
	SAFE_DELETE(g_pAuditInstanceSet);
	SAFE_DELETE(g_pAssetInstanceSet);

}

